package com.cn.kabalifeuser.ui.integral.activity;

import com.cn.kabalifeuser.api.Api;
import com.cn.kabalifeuser.base.BasePresenter;
import com.cn.kabalifeuser.callback.CacheCallBack;
import com.cn.kabalifeuser.config.Constants;
import com.cn.kabalifeuser.ui.integral.bean.DefaultAddressBean;
import com.cn.kabalifeuser.ui.integral.bean.OrderCarBean;
import com.cn.kabalifeuser.ui.integral.bean.OrderCouponsBean;
import com.cn.kabalifeuser.ui.integral.bean.OrderPlaceBean;
import com.cn.kabalifeuser.utils.LoggerUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.List;

/**
 * Created by 晓林 on 2020/9/18.
 * function:
 */
public class OrderConfirmPresenter extends BasePresenter<OrderConfirmView> {


    public void onOrderPayData(String token, String type, int good_id, int spec_id, int good_num, int address_id, String content, int coupon_id, int area_id) {
        OkGo.<OrderCarBean>post(Api.API_ORDER_PAY)
                .headers("token", token)
                .params("type", type)
                .params("good_id", good_id)
                .params("spec_id", spec_id)
                .params("good_num", good_num)
                .params("address_id", address_id)
                .params("content", content)
                .params("coupon_id", coupon_id)
                .params("area_id", area_id)
//                .params("id", id)
                .execute(new CacheCallBack<OrderCarBean>() {
                    @Override
                    public void onSuccess(Response<OrderCarBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("创建订单：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onOrderCarSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());
                    }
                });
    }

    public void onOrderPayData(String token, String type, String id, int address_id, String content, int coupon_id, int area_id) {
        OkGo.<OrderCarBean>post(Api.API_ORDER_PAY)
                .headers("token", token)
                .params("type", type)
                .params("id", id)
                .params("address_id", address_id)
                .params("content", content)
                .params("coupon_id", coupon_id)
                .params("area_id", area_id)
//                .params("id", id)
                .execute(new CacheCallBack<OrderCarBean>() {
                    @Override
                    public void onSuccess(Response<OrderCarBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("创建订单：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onOrderCarSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());
                    }
                });
    }

    /**
     * 可用优惠券
     *
     * @param token
     */
    public void onCouponsData(String token, String good_id, String type, String id, String good_num, String spec_id) {
        OkGo.<OrderCouponsBean>get(Api.API_ORDER_COUPONS)
                .headers("token", token)
                .params("good_id", good_id)
                .params("type", type)
                .params("id", id)
                .params("good_num", good_num)
                .params("spec_id", spec_id)
                .execute(new CacheCallBack<OrderCouponsBean>() {
                    @Override
                    public void onSuccess(Response<OrderCouponsBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("可用优惠券：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onCouponsSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());
                    }
                });
    }

    /**
     * 支付宝和微信
     *
     * @param token
     * @param id
     * @param pay_type
     */
    public void onOrderPayGoData(String token, int id, int pay_type) {
        OkGo.<String>post(Api.API_ORDER_GO_PAY)
                .headers("token", token)
                .params("pay_type", pay_type)
                .params("id", id)
                .execute(new CacheCallBack<String>() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("立即支付：" + response.body());
                        getView().onOrderPaySuccess(response.body());
                    }
                });
    }

    /**
     * 结算购物车
     *
     * @param token
     * @param type
     * @param id
     */
    public void onOrderData(String token, String type, String id, int coupon_id) {
        OkGo.<OrderPlaceBean>post(Api.API_ORDER_CART)
                .headers("token", token)
                .params("type", type)
                .params("id", id)
                .params("coupon_id", coupon_id)
                .execute(new CacheCallBack<OrderPlaceBean>() {
                    @Override
                    public void onSuccess(Response<OrderPlaceBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("下单：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onOrderSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());
                    }
                });
    }

    /**
     * 下单。提交订单。  单个
     *
     * @param token
     * @param type
     * @param good_id
     * @param spec_id
     * @param good_num
     */
    public void onOrderData(String token, String type, int good_id, int spec_id, int good_num, int coupon_id) {
        OkGo.<OrderPlaceBean>post(Api.API_ORDER_CART)
                .headers("token", token)
                .params("type", type)
                .params("good_id", good_id)
                .params("spec_id", spec_id)
                .params("good_num", good_num)
                .params("coupon_id", coupon_id)
                .execute(new CacheCallBack<OrderPlaceBean>() {
                    @Override
                    public void onSuccess(Response<OrderPlaceBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("下单：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onOrderSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());
                    }
                });
    }



    public void onAddressData(String token) {
        OkGo.<DefaultAddressBean>post(Api.API_ADDRESS)
                .headers("token", token)
                .execute(new CacheCallBack<DefaultAddressBean>() {
                    @Override
                    public void onSuccess(Response<DefaultAddressBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("默认地址：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onAddressSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());
                    }
                });
    }
}
